package org.lex.splitter.ui.awt;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MessageDialog extends Dialog {

	public static void main(String[] args) {
		MessageDialog.showMessage(null, "Message", "Prompt", "blabla message");
	}

	public static void showMessage(Frame parent, String title, String prompt, String message) {
		MessageDialog msg = new MessageDialog(parent);
		msg.setTitle(title);
		msg.setPrompt(prompt);
		msg.setMessage(message);
		msg.setModal(true);
		msg.setVisible(true);
	}

	private static final long serialVersionUID = -8856652285490120928L;

	private Label label = null;
	private TextArea textArea = null;

	public MessageDialog(Frame parent) {
		super(parent);
		this.initialize();
		this.setSize(350, 200);
		this.setLocationRelativeTo(parent);
	}

	public void setPrompt(String prompt) {
		this.getLabel().setText(prompt);
	}

	public void setMessage(String message) {
		this.getTextArea().setText(message);
	}

	private void initialize() {
		BorderLayout layout = new BorderLayout();
		layout.setVgap(5);
		this.setLayout(layout);

		this.add(this.getLabel(), BorderLayout.NORTH);
		this.add(this.getTextArea(), BorderLayout.CENTER);

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});
	}

	private Label getLabel() {
		if (null == label) {
			label = new Label();
		}
		return label;
	}

	private TextArea getTextArea() {
		if (null == textArea) {
			textArea = new TextArea();
			textArea.setEditable(false);
		}
		return textArea;
	}
}
